﻿var timer = null;
var hikApi = {
	option:{
		id:"",
		num:0,
		timerMap:new Map(),
		wnds:1
	},
	getActiveX : function() {
		var _this=this;
		var ocx =document.getElementById(_this.option.id);
		return ocx;
	},
	setLocalParams : function() {//设置本地参数
		_this =this;
		var ocxObj = _this.getActiveX();
		var devPxRa = screen.deviceXDPI / screen.logicalXDPI;
		var height = ocxObj.height * devPxRa;
		var width = ocxObj.width * devPxRa;
		var xml = '<?xml version="1.0" encoding="UTF-8"?> ' 
				+ '<localParam> '
				+ '<width>' + width + '</width> '
				+ '<height>' + height + '</height> '
				+ '<picType>0</picType> '
				+ '<capturePath>C:\\Hikvision</capturePath> '
				+ '<recordSize>2</recordSize> '
				+ '<recordPath>C:\\Hikvision</recordPath> '
				+ '<limitPreviewTime>0</limitPreviewTime> '
				+ '<showMsgTip>0</showMsgTip></localParam>';
		var ret = ocxObj.MPV_SetLocalParam(xml);
		return ret;
	},
	init : function() {//初始化
		$("#btn_pause").hide();
		$("#btn_stop").hide();
    	$("#btn_play").hide();
    	$("#btn_snap").hide();
    	$("#btn_record").hide();
		var _this=this;
		var ocxObj = _this.getActiveX();
		var languageType = 1;
		var ret = ocxObj.MPV_Init(languageType);
		_this.setMainMenuBar();
		_this.setMenuBar();
		_this.setPlayWindowCount();
		var localParams=_this.setLocalParams();
		if (ret != 0) {
			alert("初始化失败");
		}else if(localParams!=0){
			alert("设置本地参数失败");
		} 
	},
	startPreview:function(resourceId,planId,pushId) {//预览
		var _this=this;
		$.ajax({
			url : "/hikapi/preview",
			type : "POST",
			data : {
				"carmerId": resourceId,
				"planId":planId,
				"pushId":pushId,
			},
			success : function(data) {
				if (data&& data.data) { // 调OCX预览接口
					var ret = _this.getActiveX().MPV_StartPreview(data.data);
					if (ret != 0) {
						alert("预览失败：" + data.data);
					}
				} else {
					var vDesc = "查询预览参数失败,请检查接口参数！ ";
					alert(vDesc);
				}
			},
		 
		});
		
	},
	getResources:function(planId){
		var res = [];
		$.ajax({
			url:"/video/plan/res/"+planId+"/getResources",
			type:'post',
			async:false,
			success:function(data){
				 res = JSON.parse(data);
			}
		});
		return res;
	},
	getCurrentRes:function(planId){//得到当前正在播放的资源
		_this = this;
		return _this.getResources(planId)[_this.option.num];
	},
	beginPreview:function(playTime,planId,pushId){//开始预览
		var _this = this;
		var resources = _this.getResources(planId);
		var length = resources.length;
		if(length>0){
			_this.stopAllPreview();
			_this.startPreview(resources[_this.option.num].resourceUuid,planId,pushId);
			$("#planInfo").text("正在播放："+resources[_this.option.num].resourceName);
			$("#planInfo").removeClass("hidden");
			timer = setInterval(function() {
				_this.option.num++;
				if (_this.option.num >= length) {//预览结束
					//清除定时器
					_this.clear();
					if($("#my-plan-datagrid")){
						$("#my-plan-datagrid").datagrid("reload");
					}
					if($("#my-supervise-plan-datagrid")){
						$("#my-supervise-plan-datagrid").datagrid("reload");
					}
					$.ajax({
			    		url:"/video/plan/"+pushId+"/updateRecord",
			    		type:"POST",
			    	});
				}else{
					$("#planInfo").text("正在播放："+resources[_this.option.num].resourceName);
					_this.stopAllPreview();
					_this.startPreview(resources[_this.option.num].resourceUuid,planId,pushId);
				}
			}, playTime * 1000);
		}
	},
	setPlayWindowCount:function(){
		_this =this;
		var ret = _this.getActiveX().MPV_SetPlayWndCount(parseInt(_this.option.wnds, 10));
		if(ret!=0){
			alert("设置分屏失败!");
		}
	},
	getPlayWindowCount:function(){
		_this =this;
		return _this.getActiveX().MPV_GetPlayWndCount();
	},
	pause:function(){//暂停
		clearInterval(timer);
	},
	play:function(playTime,planId,pushId){//继续预览
		_this = this;
		var resources = _this.getResources(planId);
		var length = resources.length;
		if(length>0){
			//_this.startPreview(resources[_this.option.num].resourceUuid,planId,pushId);
			$("#planInfo").text("正在播放："+resources[_this.option.num].resourceName);
			timer = setInterval(function() {
				_this.option.num++;
				
				if (_this.option.num >= length) {
					//清除所有定时器
					_this.clear();
					if($("#my-plan-datagrid")){
						$("#my-plan-datagrid").datagrid("reload");
					}
					if($("#my-supervise-plan-datagrid")){
						$("#my-supervise-plan-datagrid").datagrid("reload");
					}
					$.ajax({
			    		url:"/video/plan/"+pushId+"/updateRecord",
			    		type:"POST",
			    		async:false,
			    	});
				}else{
					$("#planInfo").text("正在播放："+resources[_this.option.num].resourceName);
					_this.stopAllPreview();
					_this.startPreview(resources[_this.option.num].resourceUuid,planId,pushId);
				}
			}, playTime * 1000);
		}
	},
	snapPicture:function(){//截图
		_this = this;
		var ret =  _this.getActiveX().MPV_SnapShot(-1);
		if (ret != 0){
			alert("选中窗口预览抓图失败");
		}
		return ret;
	},
	startRecord:function(){//开始录像
		
	},
	stopRecord:function(){//停止录像
		
	},
	setMenuBar:function(){//隐藏插件工具栏
		_this =this; 
		var ids="6,7,14";//关闭预览按钮
		var ret =  _this.getActiveX().MPV_SetToolBar(ids);
		if (ret != 0) {
			alert("自定义播放工具条按钮失败！");
		}
	},
	setMainMenuBar:function(){
		_this =this;
		var ids="6";//全屏
		var ret =  _this.getActiveX().MPV_SetMainToolBar(ids);
		if (ret != 0) {
			alert("隐藏主工具栏失败");
		}
	},
	stopAllPreview:function(){
		_this = this;
		_this.getActiveX().MPV_StopAllPreview();
	},
	clear:function(){
		_this = this;
		clearInterval(timer);
		$("#planInfo").text("播放结束");
		$("#btn_pause").hide();
		$("#btn_record").show();
		$("#btn_play").hide();
		$("#btn_btn_snap").hide();
		$("#btn_stop").hide();
		_this.option.num =0;
		_this.stopAllPreview();
	}
}
hikApi.option={id:"spv",num:0,timerMap:new Map(),wnds:1};
hikApi.init();